package com.agan.report.controller;

import cn.hutool.core.codec.Base64;
import com.agan.report.util.DateUtils;
import com.agan.report.util.WordUtils;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.agan.report.util.WordUtils.createDocx;

/**
 * @ClassName DemoTest
 * @Description
 * @Auther agan
 * @Date 2020/8/18 2:38 下午
 **/
@Slf4j
public class DemoTest {


    private static String document="document.xml";
    private static String documentXmlRels="document.xml.rels";
    private final static String separator = File.separator;
    private final static String suffix_docx = "docx";
    private final static String suffix_doc = "doc";


    public static void main(String arg[]){

        URL basePath = WordUtils.class.getClassLoader().getResource("");
//        System.out.println("basePath.getPath() ==> " + basePath.getPath());
        String picPath = basePath.getPath() + separator + "templates" + separator + "demo" + separator;

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("titleName","Demo专题");
        dataMap.put("company","阿甘公司嘿嘿");
        dataMap.put("time", DateUtils.getCurrentTime_yyyyMMddHHmmss());

        List<String> listTile = new ArrayList<>();
        listTile.add("这是第一个标题,无递归demo版");
        listTile.add("这是第二个标题,无递归demo版");
        listTile.add("这是第三个标题,无递归demo版");
        dataMap.put("listTitle", listTile);

        List<String> picTypes = new ArrayList<>();
        picTypes.add("jpg");
        dataMap.put("picTypes", picTypes);

        List<Map<String, String>> picList = new ArrayList<>();

        Map<String, String> picMap = new HashMap<>();
        // 要按顺序，  改为base64
        //picMap.put("path", picPath + "pic1.jpg");
        picMap.put("path", convertFileToBase64(picPath + "pic1.jpg"));
        picMap.put("name", "pic1.jpg");
        picList.add(picMap);

        picMap = new HashMap<>();
        //picMap.put("path", picPath + "pic2.jpg");
        picMap.put("path", convertFileToBase64(picPath + "pic2.jpg"));
        picMap.put("name", "pic2.jpg");
        picList.add(picMap);

        picMap = new HashMap<>();
        //picMap.put("path", picPath + "pic3.jpg");
        picMap.put("path", convertFileToBase64(picPath + "pic3.jpg"));
        picMap.put("name", "pic3.jpg");
        picList.add(picMap);

        picMap = new HashMap<>();
        //picMap.put("path", picPath + "timg22.jpeg");
        picMap.put("path", convertFileToBase64(picPath + "timg22.jpeg"));
        picMap.put("name", "timg22.jpeg");
        picList.add(picMap);

        dataMap.put("picList", picList);
        List<Map<String, Object>> listTaleData = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("newSite", "小明");
        map.put("blog", "11");
        map.put("weChat", "男");
        map.put("Luntan", "五年级");
        map.put("Others", "五年级");
        map.put("totle", "1000");
        listTaleData.add(map);

        map = new HashMap<>();
        map.put("newSite", "小红");
        map.put("blog", "110");
        map.put("weChat", "男12");
        map.put("Luntan", "四年级");
        map.put("Others", "六年级");
        map.put("totle", "10002");
        listTaleData.add(map);

        map = new HashMap<>();
        map.put("newSite", "小黑");
        map.put("blog", "110");
        map.put("weChat", "男12");
        map.put("Luntan", "三年级");
        map.put("Others", "二年级");
        map.put("totle", "10002");
        listTaleData.add(map);
        dataMap.put("listTaleData", listTaleData);
        String timeStr = DateUtils.getCurrentTime_yyyyMMddHHmmssSSS();
        String docxTemplateFile = "demoCmccTemplates.docx";
        String xmlDocument = "document.xml";
        String xmlDocumentXmlRels = "document.xml.rels";
        String xmlContentTypes = "[Content_Types].xml";
        String xmlHeader = "header1.xml";//可以用来修改页眉的一些信息
        String templatePath = separator + "templates" + separator + "demo/";
        String outputFileTempPath = "temp" + separator + timeStr + separator;
        String outputFileName = timeStr + "."+suffix_docx;

        try {
            createDocx(dataMap, docxTemplateFile, xmlDocument, xmlDocumentXmlRels, xmlContentTypes,
                    xmlHeader, templatePath, outputFileTempPath, outputFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * base64转inputStream
     * @return
     */
    public static InputStream baseToInputStream(final byte[] base64byte){
        ByteArrayInputStream stream = null;
        try {
            byte[] bytes1 = Base64.decode(base64byte);
            stream = new ByteArrayInputStream(bytes1);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return stream;
    }
    /**
     * 本地文件（图片、excel等）转换成Base64字符串
     *
     * @param imgPath
     */
    public static String convertFileToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            System.out.println("文件大小（字节）="+in.available());
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组进行Base64编码，得到Base64编码的字符串
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Str = encoder.encode(data);
        return base64Str;
    }
}
